$(function(){

  var $window = $(window);

  // Disable certain links
  $('section [href^=#]').click(function (e) {
    e.preventDefault()
  });

  // popover
  $("a[data-toggle=popover]")
  .popover()
  .click(function(e) {
    e.preventDefault()
  });

  // 喜欢
  $("#toolBar .like .icon").click(function(e) {
      $el = $(this).parent();

      $.post(BASE_URL + "thumbs-up.php", {"id": $el.attr('article')},
        function(data) {
            var num = $el.find(".like-num").text();
            if (data.result) {
                if (data.action == "like") {
                    $el.find(".like-num").text(num-0+1);
                    $el.addClass('like-on');
                    $el.find(".tips .desc .cancel").removeClass("hide");
                } else if (data.action == "unlike") {
                    $el.find(".like-num").text(num-0-1);
                    $el.removeClass('like-on');
                    $el.find(".tips .desc .cancel").addClass("hide");
                }
            }
        }
      );
  });

  // 收藏
  $("#toolBar .star .icon").click(function(e) {
      $el = $(this).parent();

      $.post(BASE_URL + "star.php", {"id": $el.attr('article')},
        function(data) {
            if (data.result == 1) {
                if (data.action == "star") {
                    $el.addClass('star-on');
                    $el.find(".tips .desc .cancel").removeClass("hide");
                } else if (data.action == "unstar") {
                    $el.removeClass('star-on');
                    $el.find(".tips .desc .cancel").addClass("hide");
                }
            } else {
                alert(data.msg);
            }
        }
      );
  });

  // 摇一摇
  $("#toolBar .read-later .icon").click(function() {
      var ujian_links = $(".ujian-hook a[hidefocus=true]");
      var ujian_num = ujian_links.length;

      if (ujian_num > 3) {
          var ujian_link = ujian_links.eq(parseInt(Math.random()*(ujian_num-3)));

          ujian_links[parseInt(Math.random()*ujian_links.length)].click();
          return false;

          /*
          $(this).attr({
              "href": ujian_link.attr("href"),
              "target": ujian_link.attr("target")
          });
          return true;
          */
      } else {
          var links = $(".hot-threads ul li a, .recent-comments ul li a");
          links[parseInt(Math.random()*links.length)].click();
          return false;
        }
  });

  $(".thumbs-up:not(.disabled)").click(function(e) {
      $el = $(this);
      $.post(BASE_URL + "thumbs-up.php", {"id": $el.attr('article')},
        function(data) {
            num = $el.find(".num").text();
            if (data.result) {
                $el.find(".num").text(num-0+1);
                $el.addClass('disabled');
            }
        }
      );
  });

  $(".thumbs-down:not(.disabled)").click(function(e) {
      $el = $(this);
      $.post(BASE_URL + "thumbs-down.php", {"id": $el.attr('article')},
        function(data) {
            num = $el.find(".num").text();
            if (data.result) {
                $el.find(".num").text(num-0+1);
                $el.addClass('disabled');
            }
        }
      );
  });

  var toolBar_left = $("#aticle").offset() ? $("#aticle").offset().left-60 : 0;
  $("#toolBar").offset({"left":toolBar_left}).show();

  $("#toolBar .button").hover(
    function(){
      $(".tips", this).show();
    },function(){
      $(".tips", this).hide();
    }
  );


  $("#reflink").click(function(){
      $(this).select();
  });

  $("#btn-copy").click(function(){
      $("#reflink").select();
  });

/*
  ZeroClipboard.config({
      debug: true,
      swfPath: "http://127.0.0.1/caifujianghu/assets/js/ZeroClipboard.swf"
  });

  window.clip = new ZeroClipboard($("#btn-copy"));

  clip.on("noFlash", function (client) {
      console.log("Your browser has no Flash.");
  });

  clip.on("wrongFlash", function (client, args) {
      console.log("Flash 10.0.0+ is required but you are running Flash " + args.flashVersion.replace(/,/g, "."));
  });

  clip.on('copy', function(event) {
      console.log("copy");
      var text = $("#reflink").select().val();
      var windowsText = text.replace(/\n/g, '\r\n');
      event.clipboardData.setData('text/plain', windowsText);
  });
*/

  // 提现记录滚动
  if ($('#withdraw_info').length) {
      $('#withdraw_info').vTicker({speed: 1000, 
          pause: 1000,
          showItems: 5,
          padding:4
      });

      $('#withdraw_info').on('vticker.beforeTick', function() {
          var ul = jQuery('#withdraw_info ul');
          var interval = 800;
          var op = .20;
          ul.children('li:nth-child(3)').animate({opacity: op}, interval);
          ul.children('li:nth-child(4)').animate({opacity: 1}, interval);
          ul.children('li').css("opacity", op);
      });
  }

  // 积分明细图表
  Highcharts.getOptions().colors = ["#4bb2c5", "#EAA228", "#958c12", "#953579", "#4b5de4", "#d8b83f", "#ff5800", "#0085cc", "#c747a3", "#cddf54", "#FBD178", "#26B4E3", "#bd70c7"];
  Highcharts.setOptions({global: {useUTC: false}});
  !!$('#container_point').length && $('#container_point').highcharts({
      chart: {
          plotBackgroundColor: null,
          plotBorderWidth: null,
          plotShadow: false
      },
      title: null,
      tooltip: {
        // pointFormat: '{series.name}: <b>{point.percentage:.2f}%</b>'
      },
      plotOptions: {
          pie: {
              allowPointSelect: true,
              cursor: 'pointer',
              dataLabels: {
                  enabled: true,
                  color: '#000000',
                  connectorColor: '#000000',
                  format: '<b>{point.name}</b>: {point.percentage:.2f} %'
              }
          }
      },
      series: [{
          type: 'pie',
          name: "积分",
          data: window.data_for_point || [[]]
      }]
  });

  // 推广记录
  !!$('#container_visit').length && $('#container_visit').highcharts({
      chart:{
          type: 'spline'
      },
      title: null,
      xAxis: {
          type: 'datetime',
          dateTimeLabelFormats: { // don't display the dummy year
              day: '%Y-%m-%d',
              month: '%m-%d',
              year: '%Y'
          }
      },
      yAxis: {
          title: {
              text: '推广概况'
          },
          min: 0
      },
      tooltip: {
          formatter: function() {
                  return '<b>'+ this.series.name +'</b><br/>'+
                  Highcharts.dateFormat('%Y-%m-%d', this.x) +': '+ this.y +'次';
          }
      },
      
      series: [{
          name: '访问次数PV',
          data: window.data_for_pv,
          pointInterval: 3600 * 1000 * 24
      },
      {
          name: '访问次数IP',
          data: window.data_for_ip,
          pointInterval: 3600 * 1000 * 24
      }]
  });
});

function shake(element) {
    element.css('margin-left', element.css('margin-left'));
    
    for (var i = 1; i <= 3; i++) {
        element.animate({ 'left': (30 - 10 * i) }, 20);
        element.animate({ 'left': (2 * (30 - 10 * i)) }, 20);
    }
}
